package com._54programer.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com._54programer.entity.Repense;
import com._54programer.entity.Token;
import com._54programer.entity.User;
import com._54programer.ws.CacheKey;
import com._54programer.ws.Const;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.util.MD5Util;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/user")
@Slf4j
public class LoginController {

	@Autowired
	private HazelcastInstance Hi;

	@Autowired
	private CacheKey Ck;
	

	@GetMapping("/login")
	@ResponseBody
	public Object Login(@RequestParam String username,@RequestParam String password) throws InterruptedException {

		Repense rp = new  Repense();
		
		if(StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
			rp.setMessage_code("0000001");
			rp.setMessage("无效账号!");
			return rp;
		}
		User uuu = null;
		boolean isOk = false;
		Object element =  Hi.getMap(Const.ROBOT).get(username);
		JSONObject uu = JSONArray.parseObject(element.toString());
		uuu = JSON.toJavaObject(uu, User.class);
		if(username.equals(uuu.getUsername())) {
			isOk = true;
		}
		
		
		if(!isOk) {
			rp.setMessage_code("0000001");
			rp.setMessage("无效账号!");
			return rp;
		}
		
		
		Token tk = new Token();
		tk.setToken_uid(uuu.getUid());
		tk.setToken_username(uuu.getUsername());
		long token_expire = System.currentTimeMillis() / 1000 + 5 * 60 * 1000;//5分钟
		tk.setToken_expire(token_expire);
		String token = MD5Util.toMD5String(uuu.getUid()+uuu.getUsername()+token_expire);
		Hi.getMap(Const.TOKEN).put(token, JSON.toJSON(tk));

		rp.setMessage_code("0000000");
		rp.setMessage("success!");
		JSONObject data = new JSONObject();
		data.put("token", token);
		rp.setData(data);
		return rp;
		
	}
	

	@GetMapping("/register")
	public Object Register() throws InterruptedException {
		log.info("HazelcastInstance info :{}", Hi.getConfig());

		
		for (int i = 0; i < 500; i++) {
			Ck.putMul("name", "zhz3");
			Ck.putSet("test", "obj1", Hi.getSet(Const.SERVER_INFO));
			Ck.putSet("test", "obj2", Hi.getSet(Const.SERVER_INFO));
			Ck.putSet("test", "obj3", Hi.getSet(Const.SERVER_INFO));
		}

		//关闭当前缓存节点
		Hi.shutdown();
		
//		log.info("{}",Hi.getCacheManager().toString());
//		log.info("{}",Hi.getConfig().getCacheConfigs());
//		return Hi.getSet(Const.SERVER_INFO);
		return "shutdown HazelcastInstance success!";
//		return Ck.getMul("name");
//		return Ck.getSet("test");

	}
}
